home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / tcpcli1a / craigsam.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-07-22  |  10.9 KB  |  279 lines

  1. VERSION 5.00
  2. Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
  3. Begin VB.Form Form1 
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Intergrated TCP/IP Client Server Example Application"
  6.    ClientHeight    =   3915
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   8505
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   3915
  14.    ScaleWidth      =   8505
  15.    StartUpPosition =   3  'Windows Default
  16.    Begin MSWinsockLib.Winsock Server 
  17.       Left            =   3360
  18.       Top             =   1920
  19.       _ExtentX        =   741
  20.       _ExtentY        =   741
  21.       _Version        =   393216
  22.    End
  23.    Begin MSWinsockLib.Winsock Client 
  24.       Left            =   2880
  25.       Top             =   1920
  26.       _ExtentX        =   741
  27.       _ExtentY        =   741
  28.       _Version        =   393216
  29.    End
  30.    Begin VB.Timer ConnectionTimeout 
  31.       Enabled         =   0   'False
  32.       Interval        =   5000
  33.       Left            =   2400
  34.       Top             =   1920
  35.    End
  36.    Begin VB.Frame Frame2 
  37.       Caption         =   "Server"
  38.       Height          =   3015
  39.       Left            =   4320
  40.       TabIndex        =   1
  41.       Top             =   120
  42.       Width           =   3975
  43.       Begin VB.ListBox lstServerLog 
  44.          Height          =   840
  45.          Left            =   240
  46.          TabIndex        =   16
  47.          Top             =   1560
  48.          Width           =   3495
  49.       End
  50.       Begin VB.CommandButton cmdServerListen 
  51.          Caption         =   "Listen"
  52.          Height          =   375
  53.          Left            =   2160
  54.          TabIndex        =   6
  55.          Top             =   2520
  56.          Width           =   1575
  57.       End
  58.       Begin VB.TextBox txtServerLocalIP 
  59.          Height          =   285
  60.          Left            =   1440
  61.          Locked          =   -1  'True
  62.          TabIndex        =   5
  63.          Top             =   840
  64.          Width           =   1935
  65.       End
  66.       Begin VB.TextBox txtServerPort 
  67.          Height          =   285
  68.          Left            =   1440
  69.          TabIndex        =   4
  70.          Text            =   "2400"
  71.          Top             =   480
  72.          Width           =   1935
  73.       End
  74.       Begin VB.Label Label6 
  75.          Caption         =   "Status"
  76.          Height          =   255
  77.          Left            =   240
  78.          TabIndex        =   15
  79.          Top             =   1200
  80.          Width           =   1095
  81.       End
  82.       Begin VB.Label lblServerStatus 
  83.          Caption         =   "Server is idle"
  84.          Height          =   255
  85.          Left            =   1440
  86.          TabIndex        =   13
  87.          Top             =   1200
  88.          Width           =   1935
  89.       End
  90.       Begin VB.Label Label2 
  91.          Caption         =   "Local IP"
  92.          Height          =   255
  93.          Left            =   240
  94.          TabIndex        =   3
  95.          Top             =   840
  96.          Width           =   1575
  97.       End
  98.       Begin VB.Label Label1 
  99.          Caption         =   "Listen on Port"
  100.          Height          =   255
  101.          Left            =   240
  102.          TabIndex        =   2
  103.          Top             =   480
  104.          Width           =   1815
  105.       End
  106.    End
  107.    Begin VB.Frame Frame1 
  108.       Caption         =   "Client"
  109.       Height          =   3015
  110.       Left            =   120
  111.       TabIndex        =   0
  112.       Top             =   120
  113.       Width           =   4095
  114.       Begin VB.CommandButton cmdConnect 
  115.          Caption         =   "Connect"
  116.          Height          =   375
  117.          Left            =   2280
  118.          TabIndex        =   11
  119.          Top             =   2520
  120.          Width           =   1575
  121.       End
  122.       Begin VB.TextBox txtClientIP 
  123.          Height          =   285
  124.          Left            =   1560
  125.          TabIndex        =   10
  126.          Top             =   840
  127.          Width           =   1935
  128.       End
  129.       Begin VB.TextBox txtClientPort 
  130.          Height          =   285
  131.          Left            =   1560
  132.          TabIndex        =   9
  133.          Text            =   "2400"
  134.          Top             =   480
  135.          Width           =   1935
  136.       End
  137.       Begin VB.Label Label5 
  138.          Caption         =   "Status"
  139.          Height          =   255
  140.          Left            =   240
  141.          TabIndex        =   14
  142.          Top             =   1200
  143.          Width           =   1215
  144.       End
  145.       Begin VB.Label lblClientStatus 
  146.          Caption         =   "Not Connected"
  147.          Height          =   255
  148.          Left            =   1560
  149.          TabIndex        =   12
  150.          Top             =   1200
  151.          Width           =   1935
  152.       End
  153.       Begin VB.Label Label4 
  154.          Caption         =   "IP to connect to"
  155.          Height          =   255
  156.          Left            =   240
  157.          TabIndex        =   8
  158.          Top             =   840
  159.          Width           =   1575
  160.       End
  161.       Begin VB.Label Label3 
  162.          Caption         =   "Connect to Port"
  163.          Height          =   255
  164.          Left            =   240
  165.          TabIndex        =   7
  166.          Top             =   480
  167.          Width           =   1815
  168.       End
  169.    End
  170.    Begin VB.Label Label8 
  171.       Caption         =   "For More Information contact nramsbottom@hotmail.com"
  172.       Height          =   255
  173.       Left            =   120
  174.       TabIndex        =   18
  175.       Top             =   3480
  176.       Width           =   6255
  177.    End
  178.    Begin VB.Label Label7 
  179.       Caption         =   "Intergrated TCP/IP Client Server Example Application"
  180.       Height          =   375
  181.       Left            =   120
  182.       TabIndex        =   17
  183.       Top             =   3240
  184.       Width           =   5895
  185.    End
  186. Attribute VB_Name = "Form1"
  187. Attribute VB_GlobalNameSpace = False
  188. Attribute VB_Creatable = False
  189. Attribute VB_PredeclaredId = True
  190. Attribute VB_Exposed = False
  191. Private Sub Client_Connect()
  192. ConnectionTimeout.Enabled = False  'The Client found a server and so the timeout is avoided
  193. lblClientStatus = "Connected . . . " 'Update the server status. This wont be visible if
  194.                                      'using the localhost mode because it goes so fast,
  195.                                      'however it is noticable over the internet
  196. End Sub
  197. Private Sub Client_DataArrival(ByVal bytesTotal As Long)
  198. Dim txt As String 'Dont really need this because I dont use Option Explicit
  199. Client.GetData txt, vbString 'Get the server command
  200.     If UCase(txt) = "CLOSECONNECTION" Then 'Process command - this one terminates the connection
  201.         Client.Close 'Close client socket
  202.         lblClientStatus = "Closing Connection . . ." 'Update Client Status
  203.         cmdConnect.Enabled = True 'Enabled the Connect Button to allow the test to be re-run
  204.     End If
  205. lblClientStatus = "Not Connected" 'Update the client status
  206. End Sub
  207. Private Sub cmdConnect_Click()
  208. ConnectionTimeout.Enabled = True    'Activate the timeout timer so that if there is no connection
  209.                                     'within the specified time (I set it as 5 secs), then assume
  210.                                     'that there is no server availible.
  211. If txtClientIP = "" Then    'Make sure that there is an IP of some sort
  212.     MsgBox "Must have a server IP to connect to!", vbExclamation, "Client Error"
  213.     Exit Sub
  214. ElseIf txtClientPort = "" Then 'make sure that a port is entered
  215.     MsgBox "Must have port to connect on!!", vbExclamation, "Client Error"
  216.     Exit Sub
  217. End If
  218. If LCase(txtClientIP) = LCase("localhost") Then 'This function will allow you to enter 'localhost'
  219.                                                 'instead of typing 127.0.0.1 or your local IP
  220.     Client.RemoteHost = "127.0.0.1"
  221.     Client.RemoteHost = txtClientIP   'if not 'localhost' set as the contents of the textbox
  222. End If
  223.     Client.RemotePort = txtClientPort 'This is the server port to connect to
  224.                                       'not the Client.LocalPort (use to route)
  225.     If Client.State <> sckConnected Then 'if not connected alreay, proceed to connect
  226.         Client.Connect
  227.     Else  'Must already be connected, display error end exit sub
  228.        MsgBox "Client is already connected!", vbExclamation, "Client Error"
  229.        Exit Sub
  230.     End If
  231.     lblClientStatus = "Attempting Connection . . ." 'Update client status
  232.     cmdConnect.Enabled = False    'Disable the cutton to prevent constant clicking suring processing
  233. End Sub
  234. Private Sub cmdServerListen_Click()
  235. cmdServerListen.Enabled = False 'Disable the cutton to prevent constant clicking suring processing
  236. lblServerStatus = "Listening For Connections . . . " 'Update server status
  237. If txtServerPort = "" Then 'Validation
  238.     MsgBox "Must have port to listen on!!", vbExclamation, "Server Error"
  239.     Exit Sub
  240. End If
  241.     Server.LocalPort = Int(txtServerPort) 'Unsure is the Int() function is really needed.
  242.                                           'but it aint doing any harm 8-)
  243. If Server.State <> sckConnected Then 'if not already connected then proceed to listen
  244.     Server.Listen
  245. Else 'Must already be connected, error message and exit sub
  246.     MsgBox "Server is already connected!", vbExclamation, "Server Error"
  247.     Exit Sub
  248. End If
  249. End Sub
  250. Private Sub ConnectionTimeout_Timer()
  251.     MsgBox "Client could not find server after " & ConnectionTimeout.Interval / 1000 & " seconds.", vbExclamation, "Client Error"
  252.     'Display an error message. The math is just in case you change the timout interval
  253.     If Client.State <> sckClosed Then 'If socket is not closed then close it
  254.         Client.Close
  255.     End If
  256.     cmdConnect.Enabled = True 'Enabled button for retry
  257.     ConnectionTimeout.Enabled = False 'Disable timer to prevent 'Ghost' errors
  258.     lblClientStatus = "Not Connected" 'Upadte Client Status
  259. End Sub
  260. Private Sub Form_Load()
  261. txtServerLocalIP = Server.LocalIP 'Set default values
  262. txtClientIP = "localhost"
  263. End Sub
  264. Private Sub Server_ConnectionRequest(ByVal requestID As Long)
  265. If Server.State = sckConnected Then 'If connected then error message and exit sub
  266.     MsgBox "Server is already connected!", vbExclamation, "Server Error"
  267.     Exit Sub
  268. End If
  269.     Server.Close 'Close the connection (stop listening)
  270.     Server.Accept requestID 'Connect to client
  271.     lblServerStatus = "Processing Connection . . ." 'Update server status
  272.     lstServerLog.AddItem Time & " - " & "Client Connected" 'Add entry to log
  273.     Server.SendData "CLOSECONNECTION" 'Send termination command
  274.     DoEvents 'Allow the termination command to be sent (give client time to close socket)
  275.     Server.Close 'Shutdown connection from this end
  276.     cmdServerListen.Enabled = True 'Enabled button to allow furthur testing
  277.     lblServerStatus = "Server Idle" 'Update server status
  278. End Sub
  279.